import { GameType, Stype } from "../../../Game/Scripts/Config/GameCfg";
import { AbNames } from "../../../Game/Scripts/Config/ResConfig";
import { GameData } from "../../../Game/Scripts/Data/GameData";
import { GameApp } from "../../../Game/Scripts/GameApp";
import { GameHallMsg } from "../../../Game/Scripts/GameHall/GamHall/GameHallMsg";
import { BaseClass } from "./BaseClass";
import { ICall, SceneMgr } from "./SceneMgr";
import { UIMgr, UIType } from "./UIMgr";

// 定义一个函数类型
export type MyFunction = (param1: string) => void;

export class ViewMgr extends BaseClass {
    private static _inst: ViewMgr;
    public static get inst(): ViewMgr {
        if (this._inst == null) {
            this._inst = new ViewMgr();
        }
        return this._inst;
    }

    //链接大厅
    /**
     * 
     * @param connectCall 链接成功回调 
     */
    public ConnectToHall(connectCall:ICall = null){

        //移除游戏消息组件
        let root = globalThis.gameMgr._rootNode;
        let comMsgZt = root.getComponent('GameZtMsg');
        if(comMsgZt){
            root.removeComponent('GameZtMsg');
        }
        let comMsgPk = root.getComponent('GamePkMsg');
        if(comMsgPk){
            root.removeComponent('GamePkMsg');
        }
        let comMsgTz = root.getComponent('GameTzMsg');
        if(comMsgTz){
            root.removeComponent('GameTzMsg');
        }


        //添加大厅消息组件并连接
        let cfg = GameData.inst.hallNetCfg;
        let hallMsgCom = globalThis.gameMgr._rootNode.addComponent(GameHallMsg) as GameHallMsg;
        hallMsgCom.ConnectHall(cfg.ip,cfg.port);
        hallMsgCom.SetConectCall(connectCall);

    }

    //
    /**
     * 链接游戏 主题赛的
     * export enum GameType {
    Game_ZT = 1, //主题赛
    Game_TZ = 2, //每日挑战赛
    Game_PK = 3, //好有pk赛
     * @param type 
     */
    public ConnectToGame(type: number){
        //移除大厅消息组件
        let root = globalThis.gameMgr._rootNode;
        //移除大厅网络消息管理器
        let comMsgHall = root.getComponent('GameHallMsg');
        if(comMsgHall){
            root.removeComponent('GameHallMsg');
        }
        //移除大厅通用监听
        let GameHallCom = root.getComponent('GameHallCom');
        if(GameHallCom){
            root.removeComponent('GameHallCom');
        }

        let cfg = globalThis.gameMgr.gameNet;
        console.log('======ConnectToGame========',cfg);
        let gameMsgCom = null; 
        if(type === GameType.Game_ZT){
            gameMsgCom = root.addComponent('GameZtMsg');
            gameMsgCom.ConnectGameZt(cfg.ip,cfg.port);
            globalThis.gameMgr._GameZtMsg = gameMsgCom;
        }else if(type === GameType.Game_PK){
            gameMsgCom = root.addComponent('GamePkMsg');
        }else if(type === GameType.Game_TZ){
            gameMsgCom = root.addComponent('GameTzMsg');
        }

        // if(gameMsgCom){
        //     gameMsgCom.ConnectGameZt(cfg.ip,cfg.port);
        // }
    }

    public ConnectToGameTz(){
        //移除大厅消息组件
        let root = globalThis.gameMgr._rootNode;
        //移除大厅网络消息管理器
        let comMsgHall = root.getComponent('GameHallMsg');
        if(comMsgHall){
            root.removeComponent('GameHallMsg');
        }
        //移除大厅通用监听
        let GameHallCom = root.getComponent('GameHallCom');
        if(GameHallCom){
            root.removeComponent('GameHallCom');
        }

        let cfg = globalThis.gameMgr.gameNet;
        console.log('======ConnectToGame========',cfg);
        let gameMsgCom = root.addComponent('GameTzMsg'); 
        gameMsgCom.ConnectGameTz(cfg.ip,cfg.port);
    }


    //游戏返回到大厅界面
    public GameBackToHall(type:number){
        let cfg = GameData.inst.hallNetCfg;
        let hallMsgCom = globalThis.gameMgr._rootNode.addComponent(GameHallMsg) as GameHallMsg;
        hallMsgCom.ConnectHall(cfg.ip,cfg.port);
        //链接大厅
        // GameApp.Instance.connectNetServer(Stype.Hall,()=>{
        //     console.log('====== 游戏返回 链接大厅成功=======');
        //     if(type == GameType.Game_TZ){
        //         UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameMRTZ/GameMRTZ',UIType.Full);  
        //     }
        // });
    }

    public HallToGameTz() {
        //链接游戏服务
        // GameApp.Instance.connectNetServer(Stype.Game, () => {
        //     SceneMgr.inst.goToGame('Prefabs/GamePlay/GamePlayTz', () => {
        //         console.log('=====GameMRTZ_TZ_Ctr==onStartGameRes=======')
        //     })
        // });


    }

    public HallToGameZt() {
        //链接游戏服务
        GameApp.Instance.connectNetServer(Stype.Game, () => {
            SceneMgr.inst.goToGame('Prefabs/GameZT/GamePlayZt', () => {
                console.log('=====GamePPSuccess_Ctr==onStartGameRes=======')
            })
        });
    }
}